//修改头像的实现
mui('input[name=personalimg]')[0].addEventListener('tap',function(){
	mui('.headimgNotice')[0].style.height="100%";
})
mui('.headimgNotice li:first-child')[0].addEventListener('tap',function(){
	 galleryImg();
	 mui('.headimgNotice')[0].style.height="0";
})
mui('.headimgNotice li:nth-child(2)')[0].addEventListener('tap',function(e){
	  e.stopPropagation();
	  getImage();
	mui('.headimgNotice')[0].style.height="0";
});
mui('.headimgNotice li:nth-child(3)')[0].addEventListener('tap',function(){
	mui('.headimgNotice')[0].style.height="0";
})
//拍照 
function getImage() { 
    var cmr = plus.camera.getCamera(); 
    var res = cmr.supportedImageResolutions[0]; 
    var fmt = cmr.supportedImageFormats[0]; 
    cmr.captureImage(function(path) { 
        //plus.io.resolveLocalFileSystemURL(path, function(entry) {   
    plus.io.resolveLocalFileSystemURL(path, function(entry) { 
        var localUrl = entry.toLocalURL(); 
        uploadHead(localUrl + "?version=" + new Date().getTime()); 
    }, function(err) { 
        console.error("拍照失败：" + err.message); 
    }, { 
        index: 1 
     }); 
    }); 
}  
  
//本地相册选择 
function galleryImg() { 
    plus.gallery.pick(function(a) { 
    plus.io.resolveLocalFileSystemURL(a, function(entry) { 
        plus.io.resolveLocalFileSystemURL("_doc/", function(root) { 
        root.getFile("head.png", {}, function(file) { 
            //文件已存在 
            file.remove(function() { 
            console.log("file remove success"); 
            entry.copyTo(root, 'head.png', function(e) { 
                var e = e.fullPath + "?version=" + new Date().getTime(); 
                uploadHead(e); /*上传图片*/ 
                //变更大图预览的src 
                //目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现 
                },function(e) { 
                            console.log('copy image fail:' + e.message); 
            }); 
            }, function() { 
            console.log("delete image fail:" + e.message); 
            }); 
        }, function() { 
            //文件不存在 
            entry.copyTo(root, 'head.png', function(e) { 
            var path = e.fullPath + "?version=" + new Date().getTime(); 
            uploadHead(path); /*上传图片*/ 
            },function(e) { 
            console.log('copy image fail:' + e.message); 
            }); 
        }); 
        }, function(e) { 
        console.log("get _www folder fail"); 
        }) 
    }, function(e) { 
        console.log("读取拍照文件错误：" + e.message); 
    }); 
    }, function(a) {}, { 
    filter: "image" 
    }) 
}; 
function uploadHead(imgPath) { 
    var image = new Image(); 
    image.src = imgPath;
    console.log(imgPath);
    image.onload = function(){
    console.log(image)	
    var imgData = getBase64Image(image); 
    console.log(imgData);
    mui('.personal-smallimg')[0].style.backgroundImage="url('"+imgData+"')";
    /*在这里调用上传接口*/ 
//    mui.ajax("", { 
//        data: { 
//        img: imgData 
//        }, 
//        dataType: 'json', 
//        type: 'post', 
//        timeout: 10000, 
//        success: function(data) { 
//        mui.toast('上传成功',{ 
//            duration:'long', 
//            type:'div' 
//        }); 
//                document.getElementById('head-img').src = imgPath; 
//        document.getElementById('head-img1').src = imgPath; 
//        document.getElementById('head-img2').src=imgPath; 
//        },  
//            error: function(xhr, type, errorThrown) { 
//        mui.toast('网络异常，请稍后再试！'); 
//        } 
//    }); 
    } 
} 
//将图片压缩转成base64 
function getBase64Image(img) { 
    var canvas = document.createElement("canvas"); 
    var width = img.width; 
    var height = img.height; 
    // calculate the width and height, constraining the proportions 
    if(width > height) { 
    if(width > 100) { 
        height = Math.round(height *= 100 / width); 
        width = 100; 
    } 
    } else { 
    if(height > 100) { 
        width = Math.round(width *= 100 / height); 
        height = 100; 
    } 
    } 
    canvas.width = width; /*设置新的图片的宽度*/ 
    canvas.height = height; /*设置新的图片的长度*/ 
    var ctx = canvas.getContext("2d"); 
    ctx.drawImage(img, 0, 0, width, height); /*绘图*/ 
    var dataURL = canvas.toDataURL("image/png", 0.8); 
    return dataURL; 
} 
//退出的时候保存值
var oldback=mui.back;
mui.plusReady(function(){
	mui('.icon-fanhui')[0].addEventListener('tap',function(){
		          var nicheng=mui('#nicheng')[0].innerText;
          var usersex=mui('#sexResult')[0].innerText;
          var  usercity=mui('#cityResult')[0].innerText;
          var userbirthday=mui('#birthday')[0].innerText;
          var usersign=mui('#usersign')[0].innerText;
          var  userexp=mui('#userexp')[0].value;
          console.log(nicheng+","+usersex+","+usercity+","+userbirthday+",");
          console.log(usersign)
          console.log(userexp)
          mui.ajax('',{
          	data:{
          		nicheng:nicheng,
          		usersex:usersex,
          		usercity:usercity,
          		userbirthday:userbirthday,
          		usersign:usersign,
          		userexp:userexp,
          	},
          	dataType:'json',
          	type:'post',
          	headers:{'Content-Type':'application/json'},
          	success:function(data){
          		data=JSON.stringify(data);
          		console.log(data);
          	},
          	error:function(xhr,type,errorThrown){
				//异常处理；
				console.log(xhr)
			}
          });
	      plus.webview.currentWebview().close();
	      oldback();
	})
//手机的返回键
   plus.key.addEventListener('backbutton', function() {
	       var nicheng=mui('#nicheng')[0].innerText;
          var usersex=mui('#sexResult')[0].innerText;
          var  usercity=mui('#cityResult')[0].innerText;
          var userbirthday=mui('#birthday')[0].innerText;
          var usersign=mui('#usersign')[0].innerText;
          var  userexp=mui('#userexp')[0].value;
          console.log(nicheng+","+usersex+","+usercity+","+userbirthday+",");
          console.log(usersign)
          console.log(userexp)
          mui.ajax('',{
          	data:{
          		nicheng:nicheng,
          		usersex:usersex,
          		usercity:usercity,
          		userbirthday:userbirthday,
          		usersign:usersign,
          		userexp:userexp,
          	},
          	dataType:'json',
          	type:'post',
          	headers:{'Content-Type':'application/json'},
          	success:function(data){
          		data=JSON.stringify(data);
          		console.log(data);
          	},
          	error:function(xhr,type,errorThrown){
				//异常处理；
				console.log(xhr)
			}
          })
	        plus.webview.currentWebview().close();
	        oldback();
	 }, false);
})
